package com.nba.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.nba.bean.Tdate;
import com.nba.bean.inherit.TdateInherit;
import com.nba.dao.TdateDao;
import com.nba.util.DBUtil;

public class TdateDaoImpl implements TdateDao {
	
	JdbcTemplate jt = new JdbcTemplate(DBUtil.getDataSource());
	
	@Override
	public List<Tdate> selectTdate() {
		String sql = "SELECT * FROM tdate";
		List<Tdate> list  = jt.query(sql, new BeanPropertyRowMapper<Tdate>(Tdate.class));
		return list;
	}

	@Override
	public Tdate selectTdateById(int tdateId) {
		String sql = "SELECT * FROM tdate WHERE tdate_id = ?";
		List<Tdate> list  = jt.query(sql, new BeanPropertyRowMapper<Tdate>(Tdate.class),tdateId);
		if(list.size() == 0) {
			return null;
		}else {
			return list.get(0);
		}
	}

	@Override
	public int insertTdate(Tdate tdate) {
		String sql = "INSERT INTO tdate (team_id,tdate_time,all_hit,all_move,three_hit,three_move,free_hit,free_move,attack,defense,score,backboard,assists,snatch,nutcap,fault,breakrules,season_id) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
		return jt.update(sql, tdate.getTeamId(),tdate.getTdateTime(),tdate.getAllHit(),tdate.getAllMove(),tdate.getThreeHit(),tdate.getThreeMove(),tdate.getFreeHit(),tdate.getFreeMove(),tdate.getAttack(),tdate.getDefeanse(),tdate.getScore(),tdate.getBackboard(),tdate.getAssists(),tdate.getSnatch(),tdate.getNutcap(),tdate.getFault(),tdate.getBreakrules(),tdate.getSeasonId());
	}

	@Override
	public int deleteTdate(int tdateId) {
		String sql = "DELETE FROM tdate WHERE tdate_id = ?";
		return jt.update(sql, tdateId);
	}

	@Override
	public int updateTdate(Tdate tdate) {
		String sql = "UPDATE tdate SET team_id = ?,tdate_time = ?,all_hit =?,all_move =?,three_hit =?,three_move =?,free_hit =?,free_move =?,attack =?,defense =?,score =?,backboard =?,assists =?,snatch =?,nutcap =?,fault =?,breakrules =?,season_id =? WHERE tdate_id = ?";
		return jt.update(sql, tdate.getTeamId(),tdate.getTdateTime(),tdate.getAllHit(),tdate.getAllMove(),tdate.getThreeHit(),tdate.getThreeMove(),tdate.getFreeHit(),tdate.getFreeMove(),tdate.getAttack(),tdate.getDefeanse(),tdate.getScore(),tdate.getBackboard(),tdate.getAssists(),tdate.getSnatch(),tdate.getNutcap(),tdate.getFault(),tdate.getBreakrules(),tdate.getSeasonId(),tdate.getTdateId());
	}

	@Override
	public List<TdateInherit> selectTdateAll(String keyword, int pageNum, int pageSize) {
		String sql = "SELECT * FROM tts WHERE team_name LIKE ? OR season_name LIKE ? LIMIT ?,?";
		return jt.query(sql, new BeanPropertyRowMapper<TdateInherit>(TdateInherit.class),"%"+keyword+"%","%"+keyword+"%",(pageNum-1)*pageSize,pageSize);
	}

	@Override
	public int selectTdateTotal(String keyword) {
		String sql = "SELECT count(1) FROM tts WHERE team_name LIKE ? OR season_name LIKE ?";
		return jt.queryForObject(sql, Integer.class,"%"+keyword+"%","%"+keyword+"%");
	}

	
	
	
	
	
	
	
	
	
	
	
	
}
